package ljt.music.module.tab3;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljq.mvpframework.factory.CreatePresenter;

import java.util.List;

import ljt.music.R;
import ljt.music.bean.UserList;
import ljt.music.module.base.BaseMvpFragment;
import ljt.music.ui.playlist.PlayListActivity;

/**
 * 直播页面
 *
 * @author jiangtao
 * @date 2017/11/25
 */
@CreatePresenter(Tab3FragmentPresenter.class)
public class Tab3Fragment extends BaseMvpFragment<Tab3FragmentView, Tab3FragmentPresenter> implements Tab3FragmentView, UserListAdapter.OnItemClickLitener {
  private static Tab3Fragment instance;
  private RecyclerView recycler_view;
  private UserListAdapter adapter;

  public static Tab3Fragment getInstance() {
    if (instance == null) {
      instance = new Tab3Fragment();
    }
    return instance;
  }

  @Override
  protected int attachLayoutId() {
    return R.layout.fragment_tab3;
  }

  @Override
  protected void initView(View view) {
    recycler_view = view.findViewById(R.id.recycler_view);
    recycler_view.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    recycler_view.setAdapter(adapter = new UserListAdapter(getActivity()));
    recycler_view.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

    adapter.setOnItemClickLitener(this);
  }

  @Override
  protected void initData() {
    getMvpPresenter().request("1", "userlist");
  }

  @Override
  public void requestLoading() {

  }

  @Override
  public void resultSuccess(UserList result) {
    adapter.setUserList(result);
  }

  @Override
  public void resultFailure(String result) {

  }

  @Override
  public void onItemClick(View view, int position) {
    UserList userList = adapter.getUserList();
    if (userList!=null) {
      List<UserList.PlaylistBean> playlist = userList.getPlaylist();
      UserList.PlaylistBean playlistBean = playlist.get(position);
      Intent intent = new Intent(getActivity(), PlayListActivity.class);
      intent.putExtra("types", "playlist");
      intent.putExtra("id", String.valueOf(playlistBean.getId()));
      intent.putExtra("source", "");
      getActivity().startActivity(intent);
    }
  }

  @Override
  public void onItemLongClick(View view, int position) {

  }
}
